#delimit;
clear;
capture log close;
set more off;
/*****local
CHANGE PATH
*****/;

local out "/data";
local path3 "/data";
local pathtab "/data";
local path2 "/data";
local pathtables "/data";


use "`out'/tu_panel.dta", clear;

keep pid year state ZIP_10  County_FIPS_Code Cenus_Tract Cenus_Suffix Census_Block_Group;
ren state state_101214;
keep if year>=2010;
save "`out'/tomerge_geography_2010_12_14.dta", replace;

/*Dataset with data for 1997-2010*/
use "`path2'/final.dta", clear; /*contains all location variables from TU for years 1997 to 2010*/

drop if year==2010;
append using "`out'/tomerge_geography_2010_12_14.dta";
sort pid year;

keep pid year zip state_101214 tract county state state_county bg state ZIP_10  County_FIPS_Code Cenus_Tract Cenus_Suffix Census_Block_Group;
compress;

replace zip=ZIP_10 if year>=2010 & year<=2014;
drop ZIP_10;

replace state=string(state_101214) if year>=2010 & year<=2014;
gen state_t="";
replace state_t="02" if state=="AK";
replace state_t="01" if state=="AL";
replace state_t="05" if state=="AR";
replace state_t="04" if state=="AZ";
replace state_t="06" if state=="CA";
replace state_t="08" if state=="CO";
replace state_t="09" if state=="CT";
replace state_t="11" if state=="DC";
replace state_t="10" if state=="DE";
replace state_t="12" if state=="FL";
replace state_t="13" if state=="GA";
replace state_t="15" if state=="HI";
replace state_t="19" if state=="IA";
replace state_t="16" if state=="ID";
replace state_t="17" if state=="IL";
replace state_t="18" if state=="IN";
replace state_t="20" if state=="KS";
replace state_t="21" if state=="KY";
replace state_t="22" if state=="LA";
replace state_t="25" if state=="MA";
replace state_t="24" if state=="MD";
replace state_t="23" if state=="ME";
replace state_t="26" if state=="MI";
replace state_t="27" if state=="MN";
replace state_t="28" if state=="MS";
replace state_t="29" if state=="MO";
replace state_t="30" if state=="MT";
replace state_t="37" if state=="NC";
replace state_t="38" if state=="ND";
replace state_t="31" if state=="NE";
replace state_t="32" if state=="NV";
replace state_t="33" if state=="NH";
replace state_t="34" if state=="NJ";
replace state_t="35" if state=="NM";
replace state_t="36" if state=="NY";
replace state_t="39" if state=="OH";
replace state_t="40" if state=="OK";
replace state_t="41" if state=="OR";
replace state_t="42" if state=="PA";
replace state_t="72" if state=="PR";
replace state_t="44" if state=="RI";
replace state_t="45" if state=="SC";
replace state_t="46" if state=="SD";
replace state_t="47" if state=="TN";
replace state_t="48" if state=="TX";
replace state_t="49" if state=="UT";
replace state_t="51" if state=="VA";
replace state_t="50" if state=="VT";
replace state_t="53" if state=="WA";
replace state_t="54" if state=="WV";
replace state_t="55" if state=="WI";
replace state_t="56" if state=="WY";
replace state_t="99" if state=="AA" | state=="AE" | state=="AP" | state=="AS" | state=="GU";

replace state=state_t if state_t~="" | state_t=="99";
drop state_t state_101214;

destring state, gen(state_n);
drop state;
rename state_n state;

replace county=County_FIPS_Code if year>=2010 & year<=2014;
drop County_FIPS_Code;

replace bg=Census_Block_Group if year>=2010 & year<=2014;
drop Census_Block_Group;
rename bg block;

gen tract_101214=Cenus_Tract+"."+Cenus_Suffix;
replace tract=tract_101214 if year>=2010 & year<=2014;
drop tract_101214 Cenus_Tract Cenus_Suffix;

drop state_county;

/*******Age Variables********/;
merge m:1 pid using "`pathtab'/age_tomerge.dta";
drop if _m==2;
drop _merge;

gen TU=.;
local var="1997 1999 2001 2003 2007 2010 2012 2014";
foreach i of local var {;
replace TU=mdy(6,30,`i') if year==`i';
};
replace TU=mdy(12,31,2004) if year==2004;
replace TU=mdy(12,31,2008) if year==2008;
format TU %td;

gen age=(TU-dob)/365;
replace age=floor(age);
sort pid year;
by pid: egen first_age=min(age);

local var="state county zip tract block";
foreach j of local var {;
	gen first_`j'=`j' if age==first_age;
}; 
sort pid year;
forvalues i=18/32 {;
	foreach j of local var {;
		gen `j'_`i'=`j' if age<=`i' & age[_n+1]>`i' & pid==pid[_n+1];
	};

};
/*Doing a "max" type of function--given that these variables are strings, max function does not work--so all years for each pid has the information, so then I can drop all years except the 1st to end up with a dataset with one obs per individual with all the info*/
forvalues i=18/32 {;
	sort pid state_`i';
	foreach j of local var {;
		by pid: replace `j'_`i'=`j'_`i'[1];
	};

};

sort pid year;
by pid: keep if _n==1;
drop year tract county state block zip age_2004 dob_y dob_m dob TU age;

save "`out'/tomerge_location_variables.dta", replace;

